#!/bin/bash

echo "Start: $0 $@"
if [ $# -eq 1 ];then
    OBS_META_PATH=$1
else
    echo "Usage: $0 OBS_META_PATH"
    exit 1
fi

if [[ ! -d ${OBS_META_PATH} ]];then
    echo "obs meta folder ${OBS_META_PATH} not exists"
    exit 1
fi

cd ${OBS_META_PATH}

while read line
do
    if ! echo ${line} | grep "_meta";then
        continue
    fi
    repo=$(git diff  origin/master ${line} | grep '+' | grep 'disable' | grep 'repository' | awk -F '"' '{print $2}')
    if [ -z "${repo}" ]; then
        continue
    fi
    #line eg: <package name="pkg_name" project="prj_name">
    pkg_name=$(cat ${line} | grep 'package name=' | awk -F '"' '{print $2}')
    prj_name=$(cat ${line} | grep 'package name=' | awk -F '"' '{print $4}')
    if [[ -z "${pkg_name}" || -z "${prj_name}" ]]; then
        continue
    fi
    for tmp in $(echo ${repo})
    do
        echo "starting to wipe out binaries of project ${prj_name} and package ${pkg_name}"
        osc wipebinaries ${prj_name} ${pkg_name} -r ${tmp}
    done 

done< <(git diff --name-only origin/master)